home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 232 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Count lines in file?
  5. Date: Wed, 03 Jan 96 13:41:45 GMT
  6. Organization: none
  7. Message-ID: <820676505snz@genesis.demon.co.uk>
  8. References: <4cd4lm$j62@fountain.mindlink.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4cd4lm$j62@fountain.mindlink.net>
  15.            genew@mindlink.bc.ca "Gene Wirchenko" writes:
  16.  
  17. >Lawrence Kirby <fred@genesis.demon.co.uk> wrote:
  18. >
  19. >[Attack of the giant scissors]
  20. >
  21. >>>Oh, before someone else points it out:  One problem with getc might be if 
  22. >>>the last line of the program doesn't have a terminating newline 
  23. >>>character, i.e. it terminates with the eof.  This could cause the count 
  24. >>>to be off by one in such cases.
  25. >
  26. >>It depends on your definition of a 'line'. It is reasonable to say is that
  27. >>of the final sequence of characters is not terminated by '\n' it is not
  28. >>a valid line.
  29. >
  30. >[snip]
  31. >
  32. >     You'd run into trouble on a UNIX system where the final line ends
  33. >with the end of file.  (In my limited experience: UNIX experts, if I'm
  34. >wrong, just smile to yourself and read on.)
  35.  
  36. $ echo 'test line' | wc -l
  37.        1
  38. $ echo -n 'test line' | wc -l
  39.        0
  40.  
  41. (echo -n writes the text without a terminating newline, wc -l is the standard
  42. Unix command for counting lines in a file).
  43.  
  44. Well formed lines end with '\n' in Unix.
  45.  
  46. >     Cmm nes on, Mr. Kirby!
  47. >
  48. >     It depends on your definition of a "line".  It is reasonable to
  49. >say that if <implementation defined condition>, it is not a line.
  50.  
  51. Exactly. All I'm saying is that counting '\n's is as valid a way of counting
  52. lines as any other unless you have specific information to the contrary
  53. concerning a particular implementation.
  54.  
  55. We should be talking about counting lines from streams opened in text
  56. mode. In that case the standard says "Whether the last line requires
  57. a terminating new-line character is implementation-defined" (as we both
  58. seem to be saying).
  59.  
  60. -- 
  61. -----------------------------------------
  62. Lawrence Kirby | fred@genesis.demon.co.uk
  63. Wilts, England | 70734.126@compuserve.com
  64. -----------------------------------------
  65.